package com.rulai.generator.plugin.terminator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rulai.generator.plugin.MybatisGeneratorUtil;
import com.rulai.generator.plugin.model.Table2Model;
import com.rulai.generator.plugin.support.VelocityUtil;

/**
 * 存储层项目骨架 <功能详细描述>
 * 
 * @since [产品/模块版本]
 * @author leiyang@qding.me
 * @version v1.0 Date:2018年5月9日 上午9:53:03
 */
public class RepositoryMavenProject extends MavenSkeletonProject {

    private static final Logger LOG = LoggerFactory.getLogger(RepositoryMavenProject.class);

    /**
     * tables 表模型
     */
    private List<Table2Model> tables;

    /**
     * module 模块名
     */
    private String module;

    /**
     * packageName 包名
     */
    private String packageName;


    private static String generatorConfig_vm = "/template/generatorConfig.vm";

    public RepositoryMavenProject(String projectPath, boolean repeatDel, List<Table2Model> tables, String packageName,String module) {
        super(projectPath, repeatDel);

        this.tables = tables;

        this.packageName = packageName;
        
        this.module=module;
        
        this.setArtifactId(module + "-dao");
    }

    /**
     * 填充项目内容 <功能详细描述>
     * 
     * @author leiyang@qding.me Date:2018年5月9日 下午2:19:13
     * @throws XMLParserException
     * @throws IOException
     */
    @Override
    protected void fillingContent() throws Exception {

        LOG.info("========== 开始生成generatorConfig.xml文件 ==========");

        String generatorConfigXml = MybatisGeneratorUtil.class.getResource("/").getPath().replace("/target/classes/", "") + "/generatorConfig.xml";
        if (osInfo.isWindows()) {
            generatorConfig_vm = MybatisGeneratorUtil.class.getResource(generatorConfig_vm).getPath().replaceFirst("/",
                    "");
        } else {
            generatorConfig_vm = MybatisGeneratorUtil.class.getResource(generatorConfig_vm).getPath();
        }

        try {
            VelocityContext context = new VelocityContext();

            context.put("tables", tables);
            context.put("generator_javaModelGenerator_targetPackage", packageName + ".dao.model");
            context.put("generator_sqlMapGenerator_targetPackage", packageName + ".dao.mapper");
            context.put("generator_javaClientGenerator_targetPackage", packageName + ".dao.mapper");
            context.put("targetProject", super.getMainJavaPath());
            context.put("targetProject_sqlMap", super.getMainResPath());
            VelocityUtil.generate(generatorConfig_vm, generatorConfigXml, context);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOG.info("========== 结束生成generatorConfig.xml文件 ==========");

        LOG.info("========== 开始运行MybatisGenerator ==========");
        List<String> warnings = new ArrayList<String>();
        File configFile = new File(generatorConfigXml);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        for (String warning : warnings) {
            System.out.println(warning);
        }
        LOG.info("========== 结束运行MybatisGenerator ==========");

    }

}
